﻿using System;
using System.IO;
using System.Linq.Expressions;

namespace HardMock
{
    /// <summary>
    /// TODO : BECAREFULL of serialization for Snapshot and supplied serialization serialize result from supplied serialization
    /// TODO : See Windsor for caching in order to avoid serialization during recordKey generation
    /// TODO : defensive make test on unserializable return value or parameter 
    /// ILMerge :  c:\Program Files (x86)\Microsoft\ILMerge>ilmerge /out:c:\HardMock.dll D:\Dev\hardmock\HardMock\HardMock\bin\Release\HardMock.dll D:\Dev\hardmock\HardMock\HardMock\bin\Release\Castle.Core.dll
    /// </summary>
    public static class HardMockFacade
    {
        public static T AsRecorder<T>(this T target, params Type[] excludedTypes)
            where T : class
        {
            return new StateLessHardMock(excludedTypes).BuildRecorderOver(target);
        }

        public static T BuildMockFrom<T>(this Stream stream, params Type[] excludedTypes) where T : class
        {
            return new StateLessHardMock(excludedTypes).BuildMockFrom<T>(stream);
        }

        public static void Snapshot<T>(this T target, Stream stream)
        {
            Get<RecorderAspect, T>(target).Serialize(stream);
        }

        private static TInterceptor Get<TInterceptor, T>(T target)
        {
            return target.Get<TInterceptor>();
        }

        public static void Override<T, TR>(this T mock, Expression<Func<T, TR>> func, TR value)
        {
            Get<ReplayerAspect, T>(mock).Override(func, value);
        }
    }
}